package com.xnyzc.lhy.resource.service.impl.invoice;

import com.baiwang.bop.request.impl.invoice.impl.*;
import com.baiwang.bop.respose.entity.*;
import com.xnyzc.lhy.common.exception.InvoiceException;
import com.xnyzc.lhy.common.penum.invoice.EInvoiceErrorCode;
import com.xnyzc.lhy.resource.entity.invoice.*;
import com.xnyzc.lhy.resource.service.invoice.IInvoiceService;
import com.xnyzc.lhy.resource.util.invoice.BopInvoiceUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 电子发票服务
 * </p>
 *
 * @author haoyfa
 * @since 2019-10-24
 */
@Service
@Slf4j
public class InvoiceServiceImpl implements IInvoiceService {

    @Autowired
    BopInvoiceUtils bopInvoiceUtils;

    /**
     * 开发票
     * @param invoiceParam 开发票参数信息
     * @return
     */
    @Override
    public InvoiceOpenResult invoiceOpenRequest(InvoiceParam invoiceParam) {
        bopInvoiceUtils.initializtion();
        InvoiceOpenRequest request = null;


        try {
            request = bopInvoiceUtils.convertInvoiceOpenRequest(invoiceParam);
        } catch (Exception exception) {
            log.error("百望云开发票参数错误");
            throw InvoiceException.create(EInvoiceErrorCode.convertParamError);
        }

        InvoiceOpenResponse invoiceOpenResponse =  bopInvoiceUtils.invoiceOpenRequest(request);
        InvoiceOpenResult invoiceResult = new InvoiceOpenResult();

        if(invoiceOpenResponse != null) {
            InvoiceOpenSuccessResponse invoiceOpenSuccessResponse = new InvoiceOpenSuccessResponse();
            //发票代码
            invoiceOpenSuccessResponse.setInvoiceCode(invoiceOpenResponse.getInvoiceCode());
            //发票号码
            invoiceOpenSuccessResponse.setInvoiceNo(invoiceOpenResponse.getInvoiceNo());
            //开票日期
            invoiceOpenSuccessResponse.setInvoiceDate(invoiceOpenResponse.getInvoiceDate());
            //税控码
            invoiceOpenSuccessResponse.setTaxControlCode(invoiceOpenResponse.getTaxControlCode());
            //校验码
            invoiceOpenSuccessResponse.setInvoiceCheckCode(invoiceOpenResponse.getInvoiceCheckCode());
            //二维码
            invoiceOpenSuccessResponse.setInvoiceQrCode(invoiceOpenResponse.getInvoiceQrCode());

            invoiceResult.setIsSuccess(true);
            invoiceResult.setInvoiceOpenSuccessResponse(invoiceOpenSuccessResponse);
        }
        else
        {
            //创建未知错误返回信息
            InvoiceErrorResponse invoiceErrorResponse = new InvoiceErrorResponse(EInvoiceErrorCode.unKnow);

            invoiceResult.setIsSuccess(false);
            invoiceResult.setInvoiceErrorResponse(invoiceErrorResponse);
        }

        return invoiceResult;
    }

    /**
     * 发票查询
     * @param request 发票查询参数信息
     * @return
     */
    @Override
    public InvoiceQueryResponse invoiceQueryRequest(InvoiceQueryRequest request) {
        bopInvoiceUtils.initializtion();
        return bopInvoiceUtils.invoiceQueryRequest(request);
    }

    /**
     * 发票作废
     * @param invoiceInvalidParam 发票作废参数信息
     * @return
     */
    @Override
    public ReInvoiceInvalidResult invoiceInvalidRequest(InvoiceInvalidParam invoiceInvalidParam) {
        bopInvoiceUtils.initializtion();

        InvoiceInvalidRequest request = null;

        try {
            request = bopInvoiceUtils.convertInvoiceInvalidParam(invoiceInvalidParam);
        } catch (Exception exception) {
            log.error("百望云发票作废参数错误");
            throw InvoiceException.create(EInvoiceErrorCode.convertParamError);
        }

        InvoiceInvalidResponse invoiceInvalidResponse =  bopInvoiceUtils.invoiceInvalidRequest(request);
        ReInvoiceInvalidResult invoiceResult = new ReInvoiceInvalidResult();

        if(invoiceInvalidResponse != null) {
            InvoiceInvalidSuccessResponse invoiceInvalidSuccessResponse = new InvoiceInvalidSuccessResponse();
            //发票代码
            invoiceInvalidSuccessResponse.setInvoiceCode(invoiceInvalidResponse.getInvoiceCode());
            //发票号码
            invoiceInvalidSuccessResponse.setInvoiceNo(invoiceInvalidResponse.getInvoiceNo());
            //开票日期
            invoiceInvalidSuccessResponse.setInvoiceInvalidDate(invoiceInvalidResponse.getInvoiceInvalidDate());

            invoiceResult.setIsSuccess(true);
            invoiceResult.setInvoiceInvalidSuccessResponse(invoiceInvalidSuccessResponse);
        }
        else
        {
            //创建未知错误返回信息
            InvoiceErrorResponse invoiceErrorResponse = new InvoiceErrorResponse(EInvoiceErrorCode.unKnow);

            invoiceResult.setIsSuccess(false);
            invoiceResult.setInvoiceErrorResponse(invoiceErrorResponse);
        }

        return invoiceResult;
    }

    /**
     * 板式文件生成
     * @param invoiceFormatFileParam 板式文件生成参数信息
     * @return
     */
    @Override
    public InvoiceFormatFileResult formatfileBuildRequest(InvoiceFormatFileParam invoiceFormatFileParam) {
        bopInvoiceUtils.initializtion();

        FormatfileBuildRequest request = null;
        try {
            request = bopInvoiceUtils.convertFormatfileBuildRequest(invoiceFormatFileParam);
        } catch (Exception exception) {
            log.error("百望云板式文件生成参数错误");
            throw InvoiceException.create(EInvoiceErrorCode.convertParamError);
        }

        FormatfileBuildResponse formatfileBuildResponse = bopInvoiceUtils.formatfileBuildRequest(request);

        InvoiceFormatFileResult invoiceResult = new InvoiceFormatFileResult();

        if(formatfileBuildResponse != null) {
            //创建板式文件生成成功返回值
            FormatFileSuccessResponse formatFileSuccessResponse = new FormatFileSuccessResponse();
            formatFileSuccessResponse.setMessage(formatfileBuildResponse.getMessage());
            formatFileSuccessResponse.setFileUrl(formatfileBuildResponse.getData());

            invoiceResult.setIsSuccess(true);
            invoiceResult.setFormatFileSuccessResponse(formatFileSuccessResponse);
        }
        else
        {
            //创建未知错误返回信息
            InvoiceErrorResponse invoiceErrorResponse = new InvoiceErrorResponse(EInvoiceErrorCode.unKnow);

            invoiceResult.setIsSuccess(false);
            invoiceResult.setInvoiceErrorResponse(invoiceErrorResponse);
        }

        return invoiceResult;
    }

    /**
     * 板式文件查询
     * @param request 板式文件查询参数信息
     * @return
     */
    @Override
    public FormatfileQueryResponse formatfileQueryRequest(FormatfileQueryRequest request) {
        bopInvoiceUtils.initializtion();
        return bopInvoiceUtils.formatfileQueryRequest(request);
    }
}
